{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# 1. 原理部分\n",
    "### 目标: 求w, 使得 $Var(X_{project})=f(X)=\\frac{1}{m}\\sum_{i=1}^{m}(X_1^{(i)}w_1 + X_2^{(i)}w_2+\\dots+ X_n^{(i)}w_n)^2$ 最大"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### $\\bigtriangledown f=\n",
    "\\begin{pmatrix}\n",
    "\\frac{\\partial f}{\\partial w_1}\\\\\n",
    "\\frac{\\partial f}{\\partial w_2}\\\\\n",
    "\\cdots\\\\\n",
    "\\frac{\\partial f}{\\partial w_n}\n",
    "\\end{pmatrix}=\\frac{2}{m}\n",
    "\\begin{pmatrix}\n",
    "\\sum_{i=1}^m (X_1^{(i)}w_1 + X_2^{(i)}w_2 + \\ldots + X_n^{(i)}w_n)X_1^{(i)} \\\\\n",
    "\\sum_{i=1}^m (X_1^{(i)}w_1 + X_2^{(i)}w_2 + \\ldots + X_n^{(i)}w_n)X_2^{(i)} \\\\\n",
    "\\cdots\\\\\n",
    "\\sum_{i=1}^m (X_1^{(i)}w_1 + X_2^{(i)}w_2 + \\ldots + X_n^{(i)}w_n)X_n^{(i)} \\\\\n",
    "\\end{pmatrix}\n",
    "=\\frac{2}{m}\n",
    "\\begin{pmatrix}\n",
    "\\sum_{i=1}^m (X^{(i)}w)X_1^{(i)} \\\\\n",
    "\\sum_{i=1}^m (X^{(i)}w)X_2^{(i)} \\\\\n",
    "\\cdots \\\\\n",
    "\\sum_{i=1}^m (X^{(i)}w)X_n^{(i)} \\\\\n",
    "\\end{pmatrix}\n",
    "$\n"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### $-----> \\frac{2}{m} \\cdot (X^{(1)}w , \\ \\ X^{(2)}w, \\ \\ X^{(3)}w, \\ \\ldots, \\ \\ X^{(m)}w)\n",
    "\\cdot \\begin{pmatrix}\n",
    "X_1^{(1)} \\ \\ X_2^{(1)} \\ \\ X_3^{(1)}\\ \\ \\ldots \\ \\ X_n^{(1)}\\\\\n",
    "X_1^{(2)} \\ \\ X_2^{(2)} \\ \\ X_3^{(2)}\\ \\ \\ldots \\ \\ X_n^{(2)}\\\\\n",
    "X_1^{(3)} \\ \\ X_2^{(3)} \\ \\ X_3^{(3)}\\ \\ \\ldots \\ \\ X_n^{(3)}\\\\\n",
    "\\cdots \\ \\ \\ \\cdots \\ \\ \\ \\cdots \\ \\ \\ \\cdots \\ \\ \\ \\cdots \\\\\n",
    "X_1^{(m)} \\ \\ X_2^{(m)} \\ \\ X_3^{(m)}\\ \\ \\ldots \\ \\ X_n^{(m)}\\\\\n",
    "\\end{pmatrix}$\n",
    "\n",
    "### $=\\frac{2}{m} \\cdot (Xw)^T \\cdot X$ -----> $\\frac{2}{m}\\cdot X^T(Xw) = \\bigtriangledown f$"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# 2.使用梯度上升法求解主成分"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 32,
   "metadata": {},
   "outputs": [],
   "source": [
    "import numpy as np\n",
    "import matplotlib.pyplot as plt"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 33,
   "metadata": {},
   "outputs": [],
   "source": [
    "x = np.empty((100,2))\n",
    "x[:, 0] = np.random.uniform(0., 100., size=100)\n",
    "x[:, 1] = 0.75 * x[:, 0] + 3. + np.random.normal(0, 10., size=100)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 34,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "image/png": "iVBORw0KGgoAAAANSUhEUgAAAXQAAAD8CAYAAABn919SAAAABHNCSVQICAgIfAhkiAAAAAlwSFlzAAALEgAACxIB0t1+/AAAADl0RVh0U29mdHdhcmUAbWF0cGxvdGxpYiB2ZXJzaW9uIDIuMS4yLCBodHRwOi8vbWF0cGxvdGxpYi5vcmcvNQv5yAAAGmZJREFUeJzt3X+s3XV9x/Hnm9uirc7dIlcDF1hLJHRM5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/l5gF3Bl9LRGnTOAu/8EeMLMuncWvxT4EQ3+rOmkWi42s+XRf+vdc270Zx0Z9LneA/x5VO1yMfBMNzWTibtX+n/A24H/BP4L+PuyxxPoHP+QztetHwD7ov+9nU5O+T7gkejP08oea6Dzfwvwzejv5wL/ATwKfAN4SdnjC3C+fwDMRZ/3dmBF0z9r4JPAw8APgX8FXtK0zxq4jc4awTE6M/BrBn2udFIuX4ri2n46FUCZx6CdoiIiDVH1lIuIiKSkgC4i0hAK6CIiDaGALiLSEAroIiINoYAuItIQCugiIg2hgC4i0hD/D8APgZSBogR+AAAAAElFTkSuQmCC\n",
      "text/plain": [
       "<matplotlib.figure.Figure at 0x2e6025d1d68>"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "plt.scatter(x[:,0], x[:,1])\n",
    "plt.show()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### demean"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 35,
   "metadata": {},
   "outputs": [],
   "source": [
    "def demean(x):\n",
    "    return x - np.mean(x, axis=0)  # 在列上求mean,即 样本 - 各个特征的均值"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 36,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([1. , 1.5, 2. ])"
      ]
     },
     "execution_count": 36,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "test = np.array([[1,2,3],\n",
    "                 [1,1,1]])\n",
    "np.mean(test, axis=0)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 37,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "image/png": "iVBORw0KGgoAAAANSUhEUgAAAXwAAAD8CAYAAAB0IB+mAAAABHNCSVQICAgIfAhkiAAAAAlwSFlzAAALEgAACxIB0t1+/AAAADl0RVh0U29mdHdhcmUAbWF0cGxvdGxpYiB2ZXJzaW9uIDIuMS4yLCBodHRwOi8vbWF0cGxvdGxpYi5vcmcvNQv5y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/S7p1LpD0Yvt0uO7M7GJJ10u63N1f6nhpt6QrzexUM1un1oT1T8vYxkh+JumspHPjFLUmqHeXvE3BJbXrb0t61N2/1vHSbklXJ19fLenOorctFnff6u6rkv+Pr5S0190/Iul+SR9K3lbpfWaEX667JV2i1sTlS5I+Vu7mBPUNSadKuic5s9nn7p9094NmdpukX6hV6rnG3Y+VuJ1BuftRM/uUpD2SxiR9x90PlrxZMWyQ9FFJB8zs4eS5L0jaJuk2M/uEWh1pf13S9hXpekk7zexGSTNqHQgriSttAaAhKOkAQEMQ+ADQEAQ+ADQEgQ8ADUHgA0BDEPgA0BAEPgA0BIEPAA3x/wLDHRvz6G/YAAAAAElFTkSuQmCC\n",
      "text/plain": [
       "<matplotlib.figure.Figure at 0x2e60229ef60>"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "x_demean = demean(x)\n",
    "plt.scatter(x_demean[:,0], x_demean[:,1])\n",
    "plt.show()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 梯度上升法"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 38,
   "metadata": {},
   "outputs": [],
   "source": [
    "def f(w, x):\n",
    "    '''目标函数'''\n",
    "    return np.sum((x.dot(w))**2)/len(x)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 39,
   "metadata": {},
   "outputs": [],
   "source": [
    "def df_math(w, x):\n",
    "    return x.T.dot(x.dot(w)) * 2./len(x)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 40,
   "metadata": {},
   "outputs": [],
   "source": [
    "# 用debug函数测试梯度求导的公式是否正确\n",
    "def df_debug(w, x, epsilon=0.0001):\n",
    "    res = np.empty(len(w))\n",
    "    for i in range(len(w)):\n",
    "        w_1 = w.copy()\n",
    "        w_1[i] += epsilon\n",
    "        w_2 = w.copy()\n",
    "        w_2[i] -= epsilon\n",
    "        res[i] = (f(w_1, x) - f(w_2, x)) / (2 * epsilon)\n",
    "    return res"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 41,
   "metadata": {},
   "outputs": [],
   "source": [
    "def direction(w):\n",
    "    '''将一个向量变成单位向量'''\n",
    "    return w / np.linalg.norm(w)  # norm 求向量的模(实际上是根据ord返回范数)\n",
    "\n",
    "def gradient_ascent(df, x, initial_w, eta, n_iters=1e4, epsilon=1e-8):\n",
    "    \n",
    "    w = direction(initial_w)\n",
    "    cur_iter = 0\n",
    "    \n",
    "    while cur_iter < n_iters:\n",
    "        gradient = df(w, x)\n",
    "        last_w = w  # 我们投影的时候是投影在方向向量w上面的,即需要将w变成单位方向向量\n",
    "        w = w + eta * gradient\n",
    "        w = direction(w)   # 注意归一化为单位方向向量 [1]\n",
    "        if (abs(f(w, x) - f(last_w, x)) < epsilon):\n",
    "            break\n",
    "        cur_iter += 1\n",
    "    \n",
    "    return w"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 42,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([0.86944443, 0.34837115])"
      ]
     },
     "execution_count": 42,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 初始化的w向量不能为0 , 这点与梯度下降法求解线性回归不同, 注意[2]!\n",
    "initial_w = np.random.random(x.shape[1])\n",
    "eta = 0.001\n",
    "initial_w"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 43,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([0.77106583, 0.63675544])"
      ]
     },
     "execution_count": 43,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 注意![3]不能进行StandardScaler标准化数据\n",
    "# 因为我们的目的就是要投影在一个向量上, 然后投影的向量的方差最大\n",
    "# 如果标准化了数据, 那么方差就为1了, 固定了\n",
    "gradient_ascent(df_debug, x_demean, initial_w, eta)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 44,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([0.77106583, 0.63675544])"
      ]
     },
     "execution_count": 44,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "w = gradient_ascent(df_math, x_demean, initial_w, eta)\n",
    "w"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 45,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "image/png": "iVBORw0KGgoAAAANSUhEUgAAAXwAAAD8CAYAAAB0IB+mAAAABHNCSVQICAgIfAhkiAAAAAlwSFlzAAALEgAACxIB0t1+/AAAADl0RVh0U29mdHdhcmUAbWF0cGxvdGxpYiB2ZXJzaW9uIDIuMS4yLCBodHRwOi8vbWF0cGxvdGxpYi5vcmcvNQv5yAAAIABJREFUeJzt3XucVXW9//HXxxFtzI6TQagD/KAOoSImOpI56aMww1s4qRnWSbxFdbRj5oVBNDp5YRTLuxWnm+YNFB1NLUIBD1JeBkEREeV4ZVDBAkUZlYHv74+1B4dh7fu67b3ez8eDh+w9a++11qif/d2f7+f7+ZpzDhERqX7bxH0BIiISDQV8EZGUUMAXEUkJBXwRkZRQwBcRSQkFfBGRlFDAFxFJCQV8EZGUUMAXEUmJbeO+gO569+7tBg4cGPdliIhUlAULFrzlnOuT77hEBfyBAwfS1tYW92WIiFQUM3ulkOOU0hERSQkFfBGRlFDAFxFJCQV8EZGUUMAXEUmJRFXpiIgkWevCdqbMXMbKtR3sVlfLuaOG0DS8Pu7LKpgCvohIAVoXtjPhrsV0bNgIQPvaDibctRigYoK+Ar6ISAGmzFy2Odh36diwkSkzl20R8JP8LUABX0SkACvXduR9PunfAjRpKyJSgN3qavM+n+tbQBIo4IuIFODcUUOo7VWzxXO1vWo4d9SQzY8L+RYQJwV8EZECNA2vZ/Ixw6ivq8WA+rpaJh8zbItUTSHfAuKkHL6ISIGahtfnzMWfO2rIFjl82PpbAMQ3sauALyISkK6gnSuYxzmxq4AvIhKgfN8CCi3vDINy+CIiEYpzYlcBX0QkQnFO7AYW8M2sxswWmtl9mceDzOwxM3vBzKaZ2XZBnUtEpFL5lXcCvPdBJ60L20M9d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EUuL/A6VFMjBXP9+IAAAAAElFTkSuQmCC\n",
      "text/plain": [
       "<matplotlib.figure.Figure at 0x2e6026ca710>"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "plt.scatter(x_demean[:,0], x_demean[:,1])\n",
    "plt.plot([0, w[0]*60], [0, w[1]*60], color='r')\n",
    "plt.show()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 如果不加入噪音"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 46,
   "metadata": {},
   "outputs": [],
   "source": [
    "x2 = np.empty((100,2))\n",
    "x2[:, 0] = np.random.uniform(0., 100., size=100)\n",
    "x2[:, 1] = 0.75 * x2[:, 0] + 3."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 47,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "image/png": "iVBORw0KGgoAAAANSUhEUgAAAXQAAAD8CAYAAABn919SAAAABHNCSVQICAgIfAhkiAAAAAlwSFlzAAALEgAACxIB0t1+/AAAADl0RVh0U29mdHdhcmUAbWF0cGxvdGxpYiB2ZXJzaW9uIDIuMS4yLCBodHRwOi8vbWF0cGxvdGxpYi5vcmcvNQv5yAAAGrVJREFUeJzt3X9wXOV97/H318uSrGk6soOhtoxqksu4+cFgpzuglDsZatI6pAS7KYa40Ove64nvH+29ScOomF7fYqZ0IKMmkDvT4Y4b0ro3jrFDydrJpFEZF0+nTK1GRjjCISqBEtlrF6upleQGTRHyt3/sWSOt9sdZ7Z7ds2c/rxnP7h4deZ/lyB8ePef7PI+5OyIi0vmWtLsBIiLSHAp0EZGEUKCLiCSEAl1EJCEU6CIiCaFAFxFJCAW6iEhCKNBFRBIiVKCb2e+Z2Ukze8HM9pvZ283sajMbNrOXzOyAmV0adWNFRKQyqzVT1Mx6gb8H3uvu02Z2EPgm8FHgKXd/wsz+L3DC3R+r9nddfvnlvmbNmua0XESkSxw/fvxf3X1FrfMuCfn3XQJkzGwGWAqcBTYAvxl8fS+w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kYRQoIuIJIQCXUQkIRToIiIJ8R8O4GR5IHO9zwAAAABJRU5ErkJggg==\n",
      "text/plain": [
       "<matplotlib.figure.Figure at 0x2e6026cab38>"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "plt.scatter(x2[:,0], x2[:,1])\n",
    "plt.show()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 48,
   "metadata": {},
   "outputs": [],
   "source": [
    "x2_demean = demean(x2)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 51,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([0.80000018, 0.59999976])"
      ]
     },
     "execution_count": 51,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "w2 = gradient_ascent(df_math, x2_demean, initial_w, eta)\n",
    "w2"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 50,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "0.7499999999999999"
      ]
     },
     "execution_count": 50,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "0.6/0.8  # 正好是系数"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 52,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "image/png": "iVBORw0KGgoAAAANSUhEUgAAAXwAAAD8CAYAAAB0IB+mAAAABHNCSVQICAgIfAhkiAAAAAlwSFlzAAALEgAACxIB0t1+/AAAADl0RVh0U29mdHdhcmUAbWF0cGxvdGxpYiB2ZXJzaW9uIDIuMS4yLCBodHRwOi8vbWF0cGxvdGxpYi5vcmcvNQv5yAAAIABJREFUeJzt3Xl4lNXd//H3lxAh4BIQVAikWIsom1BTocVqXcENkaIVaUsVRfvgQ6GKslmwRcXm+WlFLS2WWlQKbhjpAhGoWMUCglERaRS1AgFFhGCVCCH5/v6YSZyEyWZmMtvndV1cydz3mbnPzfLJ4dxnMXdHRESSX7NYV0BERJqGAl9EJEUo8EVEUoQCX0QkRSjwRURShAJfRCRFKPBF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gCTgfPDvS3MsYQaylTbfbv7s8EyUwj8139+xdvClE+o+65Dst/fIczscOBpYJy7fxpo7CYvM7sY2Onu683sexWHwxRN6j/32iR14Lv7ueGOm1kv4Hjg9eA/gk7Aq2Z2GoEWQOeQ4p2A7VGuakTVdN8VzGwkcDFwjn85Ljfh77sOyX5/VZhZOoGwn+/ui4KHPzKzDu6+I9hNuTN2NYyKAcBgM7sQaAkcSaDFn2lmzYOt/KT+c69LSnbpuPsGdz/G3bu4excCYfBNd/8QWAz8ODhapz+wt+K/wcnAzAYBtwKD3X1fyKnFwJVm1sLMjifw0HptLOoYJa8AXYMjNg4j8IB6cYzrFBXBfuu5wCZ3vyfk1GJgZPD7kcCzTV23aHL3Se7eKfhv+krgH+4+AngeGBYslnT33RBJ3cL/iv4OXEjgoeU+4OrYVifiHgBaAMuC/7tZ7e43uPtGM3sCeItAV88Ydy+LYT0jyt0PmtmNQD6QBvzR3TfGuFrRMgD4EbDBzF4LHpsMzASeMLNRBEamXR6j+jW1W4GFZjYDKCDwwzAlaaatiEiKSMkuHRGRVKTAFxFJEQp8EZEUocAXEUkRCnwRkRShwBcRSREKfBGRFKHAFxFJEf8fnvvZMi6zekUAAAAASUVORK5CYII=\n",
      "text/plain": [
       "<matplotlib.figure.Figure at 0x2e602800e80>"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "plt.scatter(x2_demean[:,0], x2_demean[:,1])\n",
    "plt.plot([0, w2[0]*60], [0, w2[1]*60], color='r')\n",
    "plt.show()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.6.4"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
